<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>人员管理</title>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名：</label>
                            <div class="layui-input-inline">
                                <input id="name" type="text" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">工号：</label>
                            <div class="layui-input-inline">
                                <input id="jobNumber" type="text" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">人员类别：</label>
                            <div class="layui-input-inline">
                                <select id="classesId">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始时间：</label>
                            <div class="layui-input-inline">
                                <input id="startDate" type="date" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束时间：</label>
                            <div class="layui-input-inline">
                                <input id="endDate" type="date" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">证件号码：</label>
                            <div class="layui-input-inline">
                                <input id="number" type="text" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-primary" lay-filter="data-search-btn"
                                    data-type="reloads"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-primary" lay-filter="data-search-btn"
                                    data-type="clearsheack"> 清 空
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>

        <!--表格上方添加删除-->
        <script type="text/html" id="empTableToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <!--表格主题-->
        <table class="layui-hide" id="empTable" lay-filter="empTable"></table>

        <!--右侧操作-->
        <script type="text/html" id="empTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs layui-btn-warm" lay-event="upload">照片</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        //初始化表格加载
        var tableId = table.render({
            elem: '#empTable',
            url: '/emp/list',
            toolbar: '#empTableToolbar',
            defaultToolbar: ['filter', 'exports', 'print',],
            response: {
                statusCode: 200 //规定成功的状态码，默认：0
                , countName: 'total' //规定数据总数的字段名称，默认：count
            },
            cols: [
                [
                    {type: "checkbox", width: '4%'},
                    {field: 'jobNumber', width: '6%', title: '工号', align: "center"},
                    {field: 'name', width: '8%', title: '员工姓名', align: "center"},
                    {
                        field: 'avatar', width: '7%', title: '头像', align: "center", templet: function (res) {
                            if (res.avatar == null ) return "未上传头像";
                            return "<img style='display:inline-block;width:80%;height:100%;' lay-event='look' src='" +res.avatar + "'>";
                        }
                    },
                    {field: 'sex', width: '4%', title: '性别', align: "center"},
                    {field: 'birthday', width: '10%', title: '出生日期', align: "center"},
                    {field: 'deptName', width: '8%', title: '部门名称', align: "center"},
                    /*{field: 'jobName', width: '8%', title: '职位名称', align: "center"},*/
                    {field: 'classessName', width: '8%', title: '人员类别', align: "center"},
                    {field: 'entryDate', width: '8%', title: '入职时间', align: "center"},
                    {
                        field: 'status',width:'9%', title: '状态', align: "center", templet: function (res) {
                            var id = res.id;
                            if (res.status == 1) {
                                return "<input type='checkbox'  value = '" + id + "' lay-filter='status' lay-skin='switch' lay-text='开启|禁用' checked>"
                            } else {
                                return "<input type='checkbox'  value = '" + id + "'  lay-filter='status' lay-skin='switch' lay-text='开启|禁用'>"
                            }
                        }
                    },
                    {field: 'createDate', width: '8%', title: '创建时间', align: "center"},
                    {title: '操作', minWidth: 150, toolbar: '#empTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            id: 'empTableId',
            page: true,
            skin: 'line'
        });

        //执行方法
        var active = {
            //表格重载
            reloads: function () {
                //表格重载
                var name = $('#name');              //用户姓名
                var jobNumber = $('#jobNumber');    //工号
                var classesId = $('#classesId');    //人员类别
                var number = $('#number');          //证件号码
                var startDate = $('#startDate');    //开始时间
                var endDate = $('#endDate');        //结束时间

                //执行重载
                tableId.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "name": name.val(),
                        "jobNumber": jobNumber.val(),
                        "classesId": classesId.val(),
                        "number": number.val(),
                        "startDate": startDate.val(),
                        "endDate": endDate.val()
                    }
                }, 'data');
            },
            //清空选择框
            clearsheack: function () {
                //清空搜索框
                var name = $('#name');              //用户姓名
                var jobNumber = $('#jobNumber');    //工号
                var classesId = $('#classesId');    //人员类别
                var number = $('#number');          //证件号码
                var startDate = $('#startDate');    //开始时间
                var endDate = $('#endDate');        //结束时间

                name.val("");
                jobNumber.val("");
                classesId.val("");
                number.val("");
                startDate.val("");
                endDate.val("");

                tableId.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "name": name.val(),
                        "jobNumber": jobNumber.val(),
                        "classesId": classesId.val(),
                        "number": number.val(),
                        "startDate": startDate.val(),
                        "endDate": endDate.val()
                    }
                }, 'data');
            },
            //下拉框选择
            selectClass:function(){
                var classId = $('#classesId');
                $.ajax({
                    type: "get",
                    url: "/dictionaries/type/empType",
                    dataType: "json",
                    traditional: true,
                    success: function (res) {
                        var data = res.data;
                        data.forEach(item=>
                            classId.append(new Option(item.name,item.value))
                        );
                        form.render();
                    }
                });
            }
        };
        active.selectClass();   //执行一次下拉框数据获取

        /**
         * 头部监听事件
         */
        table.on('toolbar(empTable)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['60%', '80%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/emp/show',
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;

                            $.ajax({
                                type: "post",
                                url: "/emp/save",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg("错误,联系管理员! #" + data);
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });

            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('empTableId')
                    , data = checkStatus.data;
                if (data.length <= 0) {
                    layer.msg("未选中任何行");
                    return;
                } else {
                    var ids = [];
                    ids[0] = data[0].id;
                    var names = data[0].name;
                    for (var i = 1; i < data.length; i++) {
                        ids[i] = data[i].id;
                        names += "," + data[i].name;
                    }
                    layer.confirm('确认删除:' + names, function (index) {
                        $.ajax({
                            type: "delete",
                            url: "/emp/delete",
                            dataType: "json",
                            traditional: true,
                            data: {"ids": ids},
                            success: function (data) {
                                layer.closeAll();
                                layer.msg(data.msg);
                            },
                            error: function (data) {
                                layer.closeAll();
                                layer.msg(data);
                            }
                        });
                        layer.close(index);
                    });
                }
            }
        });

        //状态开关
        form.on('switch(status)', function (obj) {
            $.ajax({
                type: "put",
                url: "/emp/edit",
                dataType: "json",
                data: {
                    "id": obj.value,
                    "status": obj.elem.checked ? "1" : "-1"
                },
                error: function (data) {
                    layer.msg(data);
                }
            });
        });


        //右侧操作监听
        table.on('tool(empTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                //编辑
                var index = layer.open({
                    title: '编辑人员信息',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['60%', '80%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/emp/show?id='+data.id,
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;
                            files['id'] = obj.data.id;
                            $.ajax({
                                type: "put",
                                url: "/emp/edit",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg)
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg(data);
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
            } else if (obj.event === 'delete') {
                //删除
                layer.confirm('确定删除', function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/emp/delete",
                        dataType: "json",
                        data: data.id,
                        success: function (data) {
                            layer.closeAll();
                            layer.msg(data.msg);
                            obj.del();
                        },
                        error: function (data) {
                            layer.closeAll();
                            layer.msg(data);
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'upload') {
                var open = layer.open({
                    title: "照片:" + data.name,
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['720px', '340px'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/emp/upload?id='+data.id,
                    success: function (layero, index) {
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.imageInit(data.avatar);    //头像地址
                        iframeWin.id=data.id;    //id
                        iframeWin.jobNumber=data.jobNumber;    //工号
                    },
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        body.find("#upload0").click();
                        body.find("#upload1").click();
                        body.find("#upload2").click();
                        body.find("#upload3").click();
                    }
                });
            } else if (obj.event === 'look') {
                //查看照片
                layer.photos({
                    shift: 1
                    , photos: {
                        "title": "查看头像"
                        , "data": [
                            {
                                "src": data.avatar
                            }
                        ]
                    }
                    , shade: 0.01
                    , closeBtn: 1
                    , anim: 5
                });
            }
        });

        //加载自定义方法
        $('[data-type]').click(function () {
            var type = $(this).data('type');
            active[type].call(this)
        })

    });

</script>

</body>
</html>